﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.IDao;
using Chinaer.Common.Extensions;

namespace Chinaer.Dao.EF
{
    public class ZhongXinGongGaoDAO : Repository<Model.Entities.Model_ZhongXinGongGao>, IZhongXinGongGaoDAO
    {
        public override bool Add(Model.Entities.Model_ZhongXinGongGao entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Add(entity);
        }
        public override bool Delete(Model.Entities.Model_ZhongXinGongGao entity)
        {
            //entity.IsModelBaseNullThrowException();
            entity.IsDelete = true;
            return base.Update(entity);
            //return base.Delete(entity);
        }
        public override bool Delete(string id)
        {
            Model.Entities.Model_ZhongXinGongGao zhongXinGongGao = GetByID(id);
            return Delete(zhongXinGongGao);
        }
        public override int DeleteByIDs(IList<string> listID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                var queryResult = from z in context.ZhongXinGongGaos
                                  where listID.Contains(z.ID)
                                  select z;
                lock (queryResult)
                {
                    foreach (var item in queryResult)
                    {
                        context.Entry<Model.Entities.Model_ZhongXinGongGao>(item).State = System.Data.Entity.EntityState.Modified;
                        item.IsDelete = true;
                    }
                }
                return context.SaveChanges();
            }
        }
        public override bool Exists(string id)
        {
            return GetByID(id) != null ? true : false;
        }
        public override Model.Entities.Model_ZhongXinGongGao GetByID(string id)
        {
            return ChinaerDbContext.ZhongXinGongGaos.FirstOrDefault(m => m.ID == id && m.IsUse == true && m.IsDelete == false);
        }
        public override IList<Model.Entities.Model_ZhongXinGongGao> Query(string sql, int pageIndex, out int recordCount, int pageSize, params System.Data.SqlClient.SqlParameter[] sqlParams)
        {
            return base.Query(sql, pageIndex, out recordCount, pageSize, sqlParams);
        }
        public override bool Update(Model.Entities.Model_ZhongXinGongGao entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Update(entity);
        }


        public List<Model.Entities.Model_ZhongXinGongGao> GetAllZhongXinGongGao()
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.ZhongXinGongGaos.Where(m => m.IsUse == true && m.IsDelete == false).ToList();
            }
        }
        /// <summary>
        /// 根据日期范围获取中心公告列表信息
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<Model.Entities.Model_ZhongXinGongGao> GetZhongXinGongGaoByTimeSpan(DateTime beginTime, DateTime endTime)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.ZhongXinGongGaos.Where(z => z.EndTime >= beginTime && z.EndTime <= endTime && z.IsUse == true && z.IsDelete == false).ToList();
            }
        }
    }
}
